package dgut.bbm.controller;

import java.text.SimpleDateFormat;
import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import dgut.bbm.entity.GoodsPicEntity;
import dgut.bbm.to.GoodsSaveTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import dgut.bbm.entity.GoodsEntity;
import dgut.bbm.service.GoodsService;
import dgut.bbm.common.utils.PageUtils;
import dgut.bbm.common.utils.R;



/**
 * 
 *
 * @author seventeen
 * @email 1973182107@qq.com
 * @date 2023-05-06 16:56:08
 */
@RestController
@RequestMapping("bbm/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    OSS ossClient;
    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endpoint;

    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucket;
    @Value("${spring.cloud.alicloud.access-key}")
    public String accessId;
    /**
     * 阿里oss
     */

    @RequestMapping("oss/policy")
    protected R policy(){

        String host = "https://"+bucket+"."+endpoint;
        // 设置上传回调URL，即回调服务器地址，用于处理应用服务器与OSS之间的通信。OSS会在文件上传完成后，把文件上传信息通过此回调URL发送给应用服务器。
        //  String callbackUrl = "https://192.168.0.0:8888";
        // 设置上传到OSS文件的前缀，可置空此项。置空后，文件将上传至Bucket的根目录下。
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String dir = format+"/";
        Map<String, String> respMap=null;
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

            respMap = new LinkedHashMap<String, String>();
            respMap.put("accessId", accessId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 100));
            // respMap.put("expire", formatISO8601Date(expiration));
        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            System.out.println(e.getMessage());
        }
        return R.ok().put("data",respMap);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("bbm:goods:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/pageByUserId")
    public R pageByUserId(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.pageByUserId(params);

        return R.ok().put("page", page);
    }

    @GetMapping("listPic")
    public R listPic(@RequestParam("id")Long id){
        List<GoodsPicEntity>ls=goodsService.listPic(id);
        return R.ok().put("data",ls);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
  //  @RequiresPermissions("bbm:goods:info")
    public R info(@PathVariable("id") Long id){
		GoodsEntity goods = goodsService.getById(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("bbm:goods:save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }
    @RequestMapping("/saveGoods")
    // @RequiresPermissions("bbm:goods:save")
    public R saveGoods(@RequestBody GoodsSaveTo goodsSaveTo){
        goodsService.saveGoods(goodsSaveTo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("bbm:goods:update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("bbm:goods:delete")
    public R delete(@RequestBody Long[] ids){
		goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
